package smartcounters.androidclient.WebServices;

import java.util.Date;
import java.util.EnumSet;
import java.util.LinkedList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.Message;

import smartcounters.androidclient.model.AuthenticatedUserCredentials;
import smartcounters.androidclient.model.eOperationsEnum;
import smartcounters.androidclient.model.eUserPermissions;

public class InformationUpdaterWebServiceClient implements OnResultReceiver{
	private LinkedList<OnResultReceiver> m_ResultReceivers;
	private String m_URL;
	
	public InformationUpdaterWebServiceClient(String i_ServerIP) {
		m_ResultReceivers = new LinkedList<OnResultReceiver>();
		
		m_URL = "http://" + i_ServerIP + ":50171/WebServices/InformationUpdaterWebService.asmx?WSDL";
	}
	
	public void CreateAccountWithoutApartment(
			AuthenticatedUserCredentials i_AuthenticatedUserCredentials,
            String i_Country,
            String i_City,
            String i_Street,
            int i_House,
            String i_Phone,
            int[] i_UsersAssociatedWith){
		
		String NAMESPACE = "http://smartcounters.org";
		String GET_SOAP_ACTION = "http://smartcounters.org/CreateAccountWithoutApartment";
		String GET_METHOD_NAME = "CreateAccountWithoutApartment";
		
		SoapObject request = new SoapObject(NAMESPACE, GET_METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		
		SoapObject authenticatedUserIDObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserID");
		authenticatedUserIDObject.addProperty("i_AuthenticatedUserID", i_AuthenticatedUserCredentials.getID());
		SoapObject authenticatedUserPasswordObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserPassword");
		authenticatedUserPasswordObject.addProperty("i_AuthenticatedUserPassword", i_AuthenticatedUserCredentials.getPassword());
		SoapObject countryObject = new SoapObject(NAMESPACE, "i_Country");
		countryObject.addProperty("i_Country", i_Country);
		SoapObject cityObject = new SoapObject(NAMESPACE, "i_City");
		cityObject.addProperty("i_City", i_City);
		SoapObject streetObject = new SoapObject(NAMESPACE, "i_Street");
		streetObject.addProperty("i_Street", i_Street);
		SoapObject houseObject = new SoapObject(NAMESPACE, "i_House");
		houseObject.addProperty("i_House", i_House);
		SoapObject phoneObject = new SoapObject(NAMESPACE, "i_Phone");
		phoneObject.addProperty("i_Phone", i_Phone);
		SoapObject usersAssociatedWithObject = new SoapObject(NAMESPACE, "i_UsersAssociatedWith");
		
		String users = "";
		for (Integer userID : i_UsersAssociatedWith){
			users += userID.toString() + ",";
		}
		
		if (users.length() > 0){
			users = users.substring(0, users.length() - 1);
		}

		usersAssociatedWithObject.addProperty("i_UsersAssociatedWith", "'" + users + "'");
				
		request.addSoapObject(authenticatedUserIDObject);
		authenticatedUserIDObject.addSoapObject(authenticatedUserPasswordObject);
		authenticatedUserPasswordObject.addSoapObject(countryObject);
		countryObject.addSoapObject(cityObject);
		cityObject.addSoapObject(streetObject);
		streetObject.addSoapObject(houseObject);
		houseObject.addSoapObject(phoneObject);
		phoneObject.addSoapObject(usersAssociatedWithObject);
		
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(m_URL);
		
		MessageHandler handler = new MessageHandler();
		handler.RegisterResultReceiver(this);
		WebMethodAsyncInvoker webMethodAsyncInvoker = new WebMethodAsyncInvoker(androidHttpTransport, GET_SOAP_ACTION, envelope, handler, eOperationsEnum.CreateAccountWithoutApartment);
		
		Thread thread = new Thread(webMethodAsyncInvoker);
		thread.start();
	}
	
	public void CreateAccountWithApartment(
			AuthenticatedUserCredentials i_AuthenticatedUserCredentials,
            String i_Country,
            String i_City,
            String i_Street,
            int i_House,
            int i_Apartment,
            String i_Phone,
            int[] i_UsersAssociatedWith){
		
		String NAMESPACE = "http://smartcounters.org";
		String GET_SOAP_ACTION = "http://smartcounters.org/CreateAccountWithApartment";
		String GET_METHOD_NAME = "CreateAccountWithApartment";
		
		SoapObject request = new SoapObject(NAMESPACE, GET_METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		
		SoapObject authenticatedUserIDObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserID");
		authenticatedUserIDObject.addProperty("i_AuthenticatedUserID", i_AuthenticatedUserCredentials.getID());
		SoapObject authenticatedUserPasswordObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserPassword");
		authenticatedUserPasswordObject.addProperty("i_AuthenticatedUserPassword", i_AuthenticatedUserCredentials.getPassword());
		SoapObject countryObject = new SoapObject(NAMESPACE, "i_Country");
		countryObject.addProperty("i_Country", i_Country);
		SoapObject cityObject = new SoapObject(NAMESPACE, "i_City");
		cityObject.addProperty("i_City", i_City);
		SoapObject streetObject = new SoapObject(NAMESPACE, "i_Street");
		streetObject.addProperty("i_Street", i_Street);
		SoapObject houseObject = new SoapObject(NAMESPACE, "i_House");
		houseObject.addProperty("i_House", i_House);
		SoapObject apartmentObject = new SoapObject(NAMESPACE, "i_Apartment");
		apartmentObject.addProperty("i_Apartment", i_Apartment);
		SoapObject phoneObject = new SoapObject(NAMESPACE, "i_Phone");
		phoneObject.addProperty("i_Phone", i_Phone);
		SoapObject usersAssociatedWithObject = new SoapObject(NAMESPACE, "i_UsersAssociatedWith");
		
		String users = "";
		for (Integer userID : i_UsersAssociatedWith){
			users += userID.toString() + ",";
		}
		
		if (users.length() > 0){
			users = users.substring(0, users.length() - 1);
		}

		usersAssociatedWithObject.addProperty("i_UsersAssociatedWith", "'" + users + "'");
				
		request.addSoapObject(authenticatedUserIDObject);
		authenticatedUserIDObject.addSoapObject(authenticatedUserPasswordObject);
		authenticatedUserPasswordObject.addSoapObject(countryObject);
		countryObject.addSoapObject(cityObject);
		cityObject.addSoapObject(streetObject);
		streetObject.addSoapObject(houseObject);
		houseObject.addSoapObject(apartmentObject);
		apartmentObject.addSoapObject(phoneObject);
		phoneObject.addSoapObject(usersAssociatedWithObject);
		
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(m_URL);
		
		MessageHandler handler = new MessageHandler();
		handler.RegisterResultReceiver(this);
		WebMethodAsyncInvoker webMethodAsyncInvoker = new WebMethodAsyncInvoker(androidHttpTransport, GET_SOAP_ACTION, envelope, handler, eOperationsEnum.CreateAccountWithApartment);
		
		Thread thread = new Thread(webMethodAsyncInvoker);
		thread.start();
	}
	
	public void DeleteAccount(AuthenticatedUserCredentials i_AuthenticatedUserCredentials, int i_AccountID){
		String NAMESPACE = "http://smartcounters.org";
		String GET_SOAP_ACTION = "http://smartcounters.org/DeleteAccount";
		String GET_METHOD_NAME = "DeleteAccount";
		
		SoapObject request = new SoapObject(NAMESPACE, GET_METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		
		SoapObject authenticatedUserIDObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserID");
		authenticatedUserIDObject.addProperty("i_AuthenticatedUserID", i_AuthenticatedUserCredentials.getID());
		SoapObject authenticatedUserPasswordObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserPassword");
		authenticatedUserPasswordObject.addProperty("i_AuthenticatedUserPassword", i_AuthenticatedUserCredentials.getPassword());
		SoapObject accountIDObject = new SoapObject(NAMESPACE, "i_AccountID");
		accountIDObject.addProperty("i_AccountID", i_AccountID);
				
		request.addSoapObject(authenticatedUserIDObject);
		authenticatedUserIDObject.addSoapObject(authenticatedUserPasswordObject);
		authenticatedUserPasswordObject.addSoapObject(accountIDObject);
		
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(m_URL);
		
		MessageHandler handler = new MessageHandler();
		handler.RegisterResultReceiver(this);
		WebMethodAsyncInvoker webMethodAsyncInvoker = new WebMethodAsyncInvoker(androidHttpTransport, GET_SOAP_ACTION, envelope, handler, eOperationsEnum.DeleteAccount);
		
		Thread thread = new Thread(webMethodAsyncInvoker);
		thread.start();
	}
	
	public void CreateUser(
			AuthenticatedUserCredentials i_AuthenticatedUserCredentials,
            String i_Username,
            EnumSet<eUserPermissions> i_UserPermissions,
            String i_Phone,
            String i_MobilePhone,
            String i_Email,
            String i_Password,
            int[] i_AccountsAssociatedWith){
		
		String NAMESPACE = "http://smartcounters.org";
		String GET_SOAP_ACTION = "http://smartcounters.org/CreateUser";
		String GET_METHOD_NAME = "CreateUser";
		
		SoapObject request = new SoapObject(NAMESPACE, GET_METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		
		SoapObject authenticatedUserIDObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserID");
		authenticatedUserIDObject.addProperty("i_AuthenticatedUserID", i_AuthenticatedUserCredentials.getID());
		SoapObject authenticatedUserPasswordObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserPassword");
		authenticatedUserPasswordObject.addProperty("i_AuthenticatedUserPassword", i_AuthenticatedUserCredentials.getPassword());
		SoapObject usernameObject = new SoapObject(NAMESPACE, "i_Username");
		usernameObject.addProperty("i_Username", i_Username);
		
		int permissions = 0;
		
		for (eUserPermissions permission : i_UserPermissions){
			if (permission == eUserPermissions.All){
				permissions = 2097151;
				break;
			}
			else{
				permissions += Math.pow(2, permission.ordinal());
			}
		}
		
		SoapObject permissionsObject = new SoapObject(NAMESPACE, "i_UserPermissions");
		permissionsObject.addProperty("i_UserPermissions", String.valueOf(permissions));
		SoapObject phoneObject = new SoapObject(NAMESPACE, "i_Phone");
		phoneObject.addProperty("i_Phone", i_Phone);
		SoapObject mobilePhone = new SoapObject(NAMESPACE, "i_MobilePhone");
		mobilePhone.addProperty("i_City", i_MobilePhone);
		SoapObject emailObject = new SoapObject(NAMESPACE, "i_Email");
		emailObject.addProperty("i_Email", i_Email);
		SoapObject passwordObject = new SoapObject(NAMESPACE, "i_Password");
		passwordObject.addProperty("i_Password", i_Password);
		SoapObject accountsAssociatedWith = new SoapObject(NAMESPACE, "i_AccountsAssociatedWith");
		
		String accounts = "";
		for (Integer accountID : i_AccountsAssociatedWith){
			accounts += accountID.toString() + ",";
		}
		
		if (accounts.length() > 0){
			accounts = accounts.substring(0, accounts.length() - 1);
		}

		accountsAssociatedWith.addProperty("i_AccountsAssociatedWith", "'" + accounts + "'");
				
		request.addSoapObject(authenticatedUserIDObject);
		authenticatedUserIDObject.addSoapObject(authenticatedUserPasswordObject);
		authenticatedUserPasswordObject.addSoapObject(usernameObject);
		usernameObject.addSoapObject(permissionsObject);
		permissionsObject.addSoapObject(phoneObject);
		phoneObject.addSoapObject(mobilePhone);
		mobilePhone.addSoapObject(emailObject);
		emailObject.addSoapObject(passwordObject);
		passwordObject.addSoapObject(accountsAssociatedWith);
		
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(m_URL);
		
		MessageHandler handler = new MessageHandler();
		handler.RegisterResultReceiver(this);
		WebMethodAsyncInvoker webMethodAsyncInvoker = new WebMethodAsyncInvoker(androidHttpTransport, GET_SOAP_ACTION, envelope, handler, eOperationsEnum.CreateUser);
		
		Thread thread = new Thread(webMethodAsyncInvoker);
		thread.start();
	}
	
	public void DeleteUser(AuthenticatedUserCredentials i_AuthenticatedUserCredentials, int i_UserID){
		String NAMESPACE = "http://smartcounters.org";
		String GET_SOAP_ACTION = "http://smartcounters.org/DeleteUser";
		String GET_METHOD_NAME = "DeleteUser";
		
		SoapObject request = new SoapObject(NAMESPACE, GET_METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		
		SoapObject authenticatedUserIDObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserID");
		authenticatedUserIDObject.addProperty("i_AuthenticatedUserID", i_AuthenticatedUserCredentials.getID());
		SoapObject authenticatedUserPasswordObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserPassword");
		authenticatedUserPasswordObject.addProperty("i_AuthenticatedUserPassword", i_AuthenticatedUserCredentials.getPassword());
		SoapObject userIDObject = new SoapObject(NAMESPACE, "i_UserID");
		userIDObject.addProperty("i_UserID", i_UserID);
				
		request.addSoapObject(authenticatedUserIDObject);
		authenticatedUserIDObject.addSoapObject(authenticatedUserPasswordObject);
		authenticatedUserPasswordObject.addSoapObject(userIDObject);
		
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(m_URL);
		
		MessageHandler handler = new MessageHandler();
		handler.RegisterResultReceiver(this);
		WebMethodAsyncInvoker webMethodAsyncInvoker = new WebMethodAsyncInvoker(androidHttpTransport, GET_SOAP_ACTION, envelope, handler, eOperationsEnum.DeleteUser);
		
		Thread thread = new Thread(webMethodAsyncInvoker);
		thread.start();
	}
	
	public void UpdateAccountWithoutApartment(
			AuthenticatedUserCredentials i_AuthenticatedUserCredentials,
			int i_AccountID,
            String i_Country,
            String i_City,
            String i_Street,
            int i_House,
            String i_Phone,
            int[] i_UsersAssociatedWith) {
		String NAMESPACE = "http://smartcounters.org";
		String GET_SOAP_ACTION = "http://smartcounters.org/UpdateAccountWithoutApartment";
		String GET_METHOD_NAME = "UpdateAccountWithoutApartment";
		
		SoapObject request = new SoapObject(NAMESPACE, GET_METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		
		SoapObject authenticatedUserIDObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserID");
		authenticatedUserIDObject.addProperty("i_AuthenticatedUserID", i_AuthenticatedUserCredentials.getID());
		SoapObject authenticatedUserPasswordObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserPassword");
		authenticatedUserPasswordObject.addProperty("i_AuthenticatedUserPassword", i_AuthenticatedUserCredentials.getPassword());
		SoapObject accountIDObject = new SoapObject(NAMESPACE, "i_AccountID");
		accountIDObject.addProperty("i_AccountID", i_AccountID);
		SoapObject countryObject = new SoapObject(NAMESPACE, "i_Country");
		countryObject.addProperty("i_Country", i_Country);
		SoapObject cityObject = new SoapObject(NAMESPACE, "i_City");
		cityObject.addProperty("i_City", i_City);
		SoapObject streetObject = new SoapObject(NAMESPACE, "i_Street");
		streetObject.addProperty("i_Street", i_Street);
		SoapObject houseObject = new SoapObject(NAMESPACE, "i_House");
		houseObject.addProperty("i_House", i_House);
		SoapObject phoneObject = new SoapObject(NAMESPACE, "i_Phone");
		phoneObject.addProperty("i_Phone", i_Phone);
		SoapObject usersAssociatedWithObject = new SoapObject(NAMESPACE, "i_UsersAssociatedWith");
		
		String users = "";
		for (Integer userID : i_UsersAssociatedWith){
			users += userID.toString() + ",";
		}
		
		if (users.length() > 0){
			users = users.substring(0, users.length() - 1);
		}

		usersAssociatedWithObject.addProperty("i_UsersAssociatedWith", "'" + users + "'");
				
		request.addSoapObject(authenticatedUserIDObject);
		authenticatedUserIDObject.addSoapObject(authenticatedUserPasswordObject);
		authenticatedUserPasswordObject.addSoapObject(accountIDObject);
		accountIDObject.addSoapObject(countryObject);
		countryObject.addSoapObject(cityObject);
		cityObject.addSoapObject(streetObject);
		streetObject.addSoapObject(houseObject);
		houseObject.addSoapObject(phoneObject);
		phoneObject.addSoapObject(usersAssociatedWithObject);
		
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(m_URL);
		
		MessageHandler handler = new MessageHandler();
		handler.RegisterResultReceiver(this);
		WebMethodAsyncInvoker webMethodAsyncInvoker = new WebMethodAsyncInvoker(androidHttpTransport, GET_SOAP_ACTION, envelope, handler, eOperationsEnum.UpdateAccountWithoutApartment);
		
		Thread thread = new Thread(webMethodAsyncInvoker);
		thread.start();
	}
	
	public void UpdateAccountWithApartment(
			AuthenticatedUserCredentials i_AuthenticatedUserCredentials,
			int i_AccountID,
            String i_Country,
            String i_City,
            String i_Street,
            int i_House,
            int i_Apartment,
            String i_Phone,
            int[] i_UsersAssociatedWith){
		String NAMESPACE = "http://smartcounters.org";
		String GET_SOAP_ACTION = "http://smartcounters.org/UpdateAccountWithApartment";
		String GET_METHOD_NAME = "UpdateAccountWithApartment";
		
		SoapObject request = new SoapObject(NAMESPACE, GET_METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		
		SoapObject authenticatedUserIDObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserID");
		authenticatedUserIDObject.addProperty("i_AuthenticatedUserID", i_AuthenticatedUserCredentials.getID());
		SoapObject authenticatedUserPasswordObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserPassword");
		authenticatedUserPasswordObject.addProperty("i_AuthenticatedUserPassword", i_AuthenticatedUserCredentials.getPassword());
		SoapObject accountIDObject = new SoapObject(NAMESPACE, "i_AccountID");
		accountIDObject.addProperty("i_AccountID", i_AccountID);
		SoapObject countryObject = new SoapObject(NAMESPACE, "i_Country");
		countryObject.addProperty("i_Country", i_Country);
		SoapObject cityObject = new SoapObject(NAMESPACE, "i_City");
		cityObject.addProperty("i_City", i_City);
		SoapObject streetObject = new SoapObject(NAMESPACE, "i_Street");
		streetObject.addProperty("i_Street", i_Street);
		SoapObject houseObject = new SoapObject(NAMESPACE, "i_House");
		houseObject.addProperty("i_House", i_House);
		SoapObject apartmentObject = new SoapObject(NAMESPACE, "i_Apartment");
		apartmentObject.addProperty("i_Apartment", i_Apartment);
		SoapObject phoneObject = new SoapObject(NAMESPACE, "i_Phone");
		phoneObject.addProperty("i_Phone", i_Phone);
		SoapObject usersAssociatedWithObject = new SoapObject(NAMESPACE, "i_UsersAssociatedWith");
		
		String users = "";
		for (Integer userID : i_UsersAssociatedWith){
			users += userID.toString() + ",";
		}
		
		if (users.length() > 0){
			users = users.substring(0, users.length() - 1);
		}

		usersAssociatedWithObject.addProperty("i_UsersAssociatedWith", "'" + users + "'");
				
		request.addSoapObject(authenticatedUserIDObject);
		authenticatedUserIDObject.addSoapObject(authenticatedUserPasswordObject);
		authenticatedUserPasswordObject.addSoapObject(accountIDObject);
		accountIDObject.addSoapObject(countryObject);
		countryObject.addSoapObject(cityObject);
		cityObject.addSoapObject(streetObject);
		streetObject.addSoapObject(houseObject);
		houseObject.addSoapObject(apartmentObject);
		apartmentObject.addSoapObject(phoneObject);
		phoneObject.addSoapObject(usersAssociatedWithObject);
		
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(m_URL);
		
		MessageHandler handler = new MessageHandler();
		handler.RegisterResultReceiver(this);
		WebMethodAsyncInvoker webMethodAsyncInvoker = new WebMethodAsyncInvoker(androidHttpTransport, GET_SOAP_ACTION, envelope, handler, eOperationsEnum.UpdateAccountWithApartment);
		
		Thread thread = new Thread(webMethodAsyncInvoker);
		thread.start();
	}
	
	public void UpdateUser(
			AuthenticatedUserCredentials i_AuthenticatedUserCredentials,
            int i_UserID,
            String i_Username,
            EnumSet<eUserPermissions> i_UserPermissions,
            String i_Phone,
            String i_MobilePhone,
            String i_Email,
            String i_Password,
            int[] i_AccountsAssociatedWith){
		String NAMESPACE = "http://smartcounters.org";
		String GET_SOAP_ACTION = "http://smartcounters.org/UpdateUser";
		String GET_METHOD_NAME = "UpdateUser";
		
		SoapObject request = new SoapObject(NAMESPACE, GET_METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		
		SoapObject authenticatedUserIDObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserID");
		authenticatedUserIDObject.addProperty("i_AuthenticatedUserID", i_AuthenticatedUserCredentials.getID());
		SoapObject authenticatedUserPasswordObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserPassword");
		authenticatedUserPasswordObject.addProperty("i_AuthenticatedUserPassword", i_AuthenticatedUserCredentials.getPassword());
		SoapObject userIDObject = new SoapObject(NAMESPACE, "i_UserID");
		userIDObject.addProperty("i_UserID", i_UserID);
		SoapObject usernameObject = new SoapObject(NAMESPACE, "i_Username");
		usernameObject.addProperty("i_Username", i_Username);
		
		int permissions = 0;
		
		for (eUserPermissions permission : i_UserPermissions){
			if (permission == eUserPermissions.All){
				permissions = 2097151;
				break;
			}
			else{
				permissions += Math.pow(2, permission.ordinal());
			}
		}
		
		SoapObject permissionsObject = new SoapObject(NAMESPACE, "i_UserPermissions");
		permissionsObject.addProperty("i_UserPermissions", String.valueOf(permissions));
		SoapObject phoneObject = new SoapObject(NAMESPACE, "i_Phone");
		phoneObject.addProperty("i_Phone", i_Phone);
		SoapObject mobilePhone = new SoapObject(NAMESPACE, "i_MobilePhone");
		mobilePhone.addProperty("i_City", i_MobilePhone);
		SoapObject emailObject = new SoapObject(NAMESPACE, "i_Email");
		emailObject.addProperty("i_Email", i_Email);
		SoapObject passwordObject = new SoapObject(NAMESPACE, "i_Password");
		passwordObject.addProperty("i_Password", i_Password);
		SoapObject accountsAssociatedWith = new SoapObject(NAMESPACE, "i_AccountsAssociatedWith");
		
		String accounts = "";
		for (Integer accountID : i_AccountsAssociatedWith){
			accounts += accountID.toString() + ",";
		}
		
		if (accounts.length() > 0){
			accounts = accounts.substring(0, accounts.length() - 1);
		}

		accountsAssociatedWith.addProperty("i_AccountsAssociatedWith", "'" + accounts + "'");
				
		request.addSoapObject(authenticatedUserIDObject);
		authenticatedUserIDObject.addSoapObject(authenticatedUserPasswordObject);
		authenticatedUserPasswordObject.addSoapObject(userIDObject);
		userIDObject.addSoapObject(usernameObject);
		usernameObject.addSoapObject(permissionsObject);
		permissionsObject.addSoapObject(phoneObject);
		phoneObject.addSoapObject(mobilePhone);
		mobilePhone.addSoapObject(emailObject);
		emailObject.addSoapObject(passwordObject);
		passwordObject.addSoapObject(accountsAssociatedWith);
		
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(m_URL);
		
		MessageHandler handler = new MessageHandler();
		handler.RegisterResultReceiver(this);
		WebMethodAsyncInvoker webMethodAsyncInvoker = new WebMethodAsyncInvoker(androidHttpTransport, GET_SOAP_ACTION, envelope, handler, eOperationsEnum.UpdateUser);
		
		Thread thread = new Thread(webMethodAsyncInvoker);
		thread.start();
	}
	
	public void UpdateUsage(int i_AuthenticatedUserID, String i_AuthenticatedUserPassword, int i_AccountID, double i_TotalUsage){
		
	}
	
	public void CreateFault(AuthenticatedUserCredentials i_AuthenticatedUserCredentials, int i_AccountID, String i_Description){
		
		String NAMESPACE = "http://smartcounters.org";
		String GET_SOAP_ACTION = "http://smartcounters.org/CreateFault";
		String GET_METHOD_NAME = "CreateFault";
		
		SoapObject request = new SoapObject(NAMESPACE, GET_METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		
		SoapObject authenticatedUserIDObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserID");
		authenticatedUserIDObject.addProperty("i_AuthenticatedUserID", i_AuthenticatedUserCredentials.getID());
		SoapObject authenticatedUserPasswordObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserPassword");
		authenticatedUserPasswordObject.addProperty("i_AuthenticatedUserPassword", i_AuthenticatedUserCredentials.getPassword());
		SoapObject accountIDObject = new SoapObject(NAMESPACE, "i_AccountID");
		accountIDObject.addProperty("i_AccountID", i_AccountID);
		SoapObject descriptionObject = new SoapObject(NAMESPACE, "i_Description");
		descriptionObject.addProperty("i_Description", i_Description);
				
		request.addSoapObject(authenticatedUserIDObject);
		authenticatedUserIDObject.addSoapObject(authenticatedUserPasswordObject);
		authenticatedUserPasswordObject.addSoapObject(accountIDObject);
		accountIDObject.addSoapObject(descriptionObject);
		
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(m_URL);
		
		MessageHandler handler = new MessageHandler();
		handler.RegisterResultReceiver(this);
		WebMethodAsyncInvoker webMethodAsyncInvoker = new WebMethodAsyncInvoker(androidHttpTransport, GET_SOAP_ACTION, envelope, handler, eOperationsEnum.CreateFault);
		
		Thread thread = new Thread(webMethodAsyncInvoker);
		thread.start();
	}
	
	public void UpdateFault(int i_AuthenticatedUserID, String i_AuthenticatedUserPassword, int i_FaultID, int i_UserID, int i_AccountID, Date i_FaultDate, String i_Description, boolean i_IsSolved){
		
	}
	
	public void SolveFault(int i_AuthenticatedUserID, String i_AuthenticatedUserPassword, int i_FaultID){
		
	}
	
	public void RegisterResultReceiver(OnResultReceiver i_ResultReceiver ) {
		m_ResultReceivers.add(i_ResultReceiver);
	}
	
	protected void OnResultReceived(Message i_Msg){
		if (i_Msg.obj instanceof ConnectionError){
			return;
		}
		
		SoapObject resultsRequestSOAP = (SoapObject) i_Msg.obj;
		if (resultsRequestSOAP.getPropertyCount() == 0) {
			i_Msg.obj = null;
		}
		else{	
			if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.CreateAccountWithoutApartment){
				i_Msg.obj = Integer.parseInt(resultsRequestSOAP.getProperty(0).toString());
			}
			else if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.CreateAccountWithApartment){
				i_Msg.obj = Integer.parseInt(resultsRequestSOAP.getProperty(0).toString());
			}
			else if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.CreateUser){
				i_Msg.obj = Integer.parseInt(resultsRequestSOAP.getProperty(0).toString());
			}
			else if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.UpdateUser){
				i_Msg.obj = true;
			}
			else if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.UpdateAccountWithoutApartment){
				i_Msg.obj = true;
			}
			else if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.UpdateAccountWithApartment){
				i_Msg.obj = true;
			}
			else if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.DeleteAccount){
				i_Msg.obj = true;
			}
			else if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.DeleteUser){
				i_Msg.obj = true;
			}
			else if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.CreateFault){
				i_Msg.obj = Integer.parseInt(resultsRequestSOAP.getProperty(0).toString());
			}
		}
		
		for (OnResultReceiver resultReceiver : m_ResultReceivers) {
			resultReceiver.ReceiveResult(i_Msg);
		}
	}

	public void ReceiveResult(Message i_Msg) {
		OnResultReceived(i_Msg);
	}
}